<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class TqvipController extends Controller
{
    //add
    public function add()
    {
    	$data = DB::table('tqvip') -> get();
    	return view('admin.tqvip.add',['data' => $data]);
    }

    //insert
    public function insert(Request $request)
    {
    	$this -> validate($request,[
            'pid' => 'required',
            'type' => 'required',
            'price' => 'required',
            ],[
            'name.required' => '名称不能为空',
            'type.required' => '期限不能为空',
            'price.required' => '价格不能为空',
            ]);
        
    	$data = $request -> except('_token');
        if($request -> pid == 1)
        {
            $data['name'] = '超级会员开通';
        }elseif($request -> pid == 2)
        {
            $data['name'] = '超级会员福利';
        }elseif($request -> pid == 3)
        {
            $data['name'] = '年费白金会员';
        }elseif($request -> pid == 4)
        {
            $data['name'] = '年费超级会员';
        }elseif($request -> pid == 5)
        {
            $data['name'] = '年费年费豪华套餐';
        }elseif($request -> pid == 6)
        {
            $data['name'] = '年费会员福利';
        }
       
    	 //处理图片
    	if($request -> hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                //获取图片扩展名
                $extension = $request -> file('pic') -> getClientOriginalExtension();

                //拼接图片名称
                $fileName = str_random(32).'.'.$extension;
                $dir = './uploads/imgs/';

                //判断目录是否存在
                if(!file_exists($dir))
                {
                    mkdir($dir,0777,true);
                }

                //移动
                $move = $request -> file('pic') -> move($dir,$fileName);

                //判断是否移动成功
                if($move)
                {
                    $data['pic'] = $fileName;
                    
                }
            }
        }

        $res = DB::table('tqvip') -> insert($data);

        if($res)
        {
        	return redirect('/admin/tqvip/index') -> with(['info' => '添加成功']);
        }else
        {
        	return back() -> with(['info' => '添加失败']);
        }
    }

    //index
    public function index()
    {
    	$data = DB::table('tqvip') -> get();
    	return view('admin.tqvip.index',['data' => $data]);
    }

    //edit
    public function edit($id)
    {
    	$data = DB::table('tqvip') -> where('id',$id) -> first();
    	return view('admin.tqvip.edit',['data' => $data]);
    }
    //update
    public function update(Request $request,$id)
    {
    	$this -> validate($request,[
            'name' => 'required',
            'type' => 'required',
            'price' => 'required',
            ],[
            'name.required' => '名称不能为空',
            'type.required' => '期限不能为空',
            'price.required' => '价格不能为空',
            ]);
    	$data = $request -> except('_token');
    	//处理图片
    	if($request -> hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                //获取图片扩展名
                $extension = $request -> file('pic') -> getClientOriginalExtension();

                //拼接图片名称
                $fileName = str_random(32).'.'.$extension;
                $dir = './uploads/imgs/';

                //判断目录是否存在
                if(!file_exists($dir))
                {
                    mkdir($dir,0777,true);
                }

                //移动
                $move = $request -> file('pic') -> move($dir,$fileName);

                //判断是否移动成功
                if($move)
                {
                    $data['pic'] = $fileName;
                    
                }
            }
        }

    	$res = DB::table('tqvip') -> where('id',$id) -> update($data);
    	if($res)
    	{
    		return redirect('admin/tqvip/index') -> with(['info' => '更新成功']);
    	}else
    	{
    		return back() -> with(['info' => '更新失败']);
    	}

    }

    //delete
    public function delete($id)
    {
    	$res = DB::table('tqvip') -> delete($id);
    	if($res)
    	{
    		return redirect('admin/tqvip/index') -> with(['info' => '删除成功']);
    	}else
    	{
    		return back() -> with(['info' => '删除失败']);
    	}
    }
}
